﻿using LanguagePolish.EntityModel;
using LanguagePolish.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LanguagePolish.Views
{
    /// <summary>
    /// Interaction logic for AddUserView.xaml
    /// </summary>
    public partial class AddUserView : Window
    {
        private readonly AddUserViewModel vm;
        bool isEdit;
        public AddUserView(User user, int teacherID, bool isEdit = false)
        {

            this.isEdit = isEdit;
            user.TeacherID = teacherID;
            user.AccountTypeID = 2;
            user.UserPassword = Crypt.Decrypt(user.UserPassword, "pshe");
            vm = new AddUserViewModel(user, isEdit);
            DataContext = vm;
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {

            string password = Crypt.Encrypt(this.pass.Text, "pshe");
            vm.User.UserPassword = password;
            var context = LinksProvider.Model.EntityContext;
            if (!isEdit)
            {
                context.Users.Add(vm.User);
            }
            context.SaveChanges();

            DialogResult = true;
        }

       
    }
}
